package com.runen.infinitemovement.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.chad.library.adapter.base.listener.OnLoadMoreListener;
import com.google.gson.reflect.TypeToken;
import com.runen.infinitemovement.R;
import com.runen.infinitemovement.adapter.FireRankingAdapter;
import com.runen.infinitemovement.adapter.TeamFireRankingAdapter;
import com.runen.infinitemovement.base.BaseActivity;
import com.runen.infinitemovement.model.api.ChallengeAPI;
import com.runen.infinitemovement.model.entity.CompanyHozomRankEntity;
import com.runen.infinitemovement.model.entity.RankDtoListEntity;
import com.runen.infinitemovement.model.entity.TeamRankEntity;
import com.runen.infinitemovement.tool.CommonUtil;
import com.runen.infinitemovement.tool.GlideUtils;
import com.runen.infinitemovement.widget.CustomLoadMoreView;
import com.runen.infinitemovement.widget.imageview.RoundedCornersImage;
import com.runen.rxnetlibrary.net.callback.BaseResultCallback;
import com.runen.rxnetlibrary.utils.JsonUtils;
import com.runen.rxnetlibrary.utils.ToastUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class RankingActivity extends BaseActivity implements SwipeRefreshLayout.OnRefreshListener {

    @BindView(R.id.btn_finish)
    Button btnFinish;
    @BindView(R.id.iv_name)
    ImageView ivName;
    @BindView(R.id.tv_progress)
    TextView tvProgress;
    @BindView(R.id.rv_ranking)
    RecyclerView rvRanking;
    @BindView(R.id.tv_ranking)
    TextView tvRanking;
    @BindView(R.id.rci_user_header)
    RoundedCornersImage rciUserHeader;
    @BindView(R.id.tv_user_name)
    TextView tvUserName;
    @BindView(R.id.tv_fire_num)
    TextView tvFireNum;
    @BindView(R.id.ll_bg)
    LinearLayout llBg;
    @BindView(R.id.ll_bottom)
    LinearLayout llBottom;
    @BindView(R.id.rv_ranking1)
    RecyclerView rvRanking1;
    @BindView(R.id.rv_ranking2)
    RecyclerView rvRanking2;
    @BindView(R.id.iv_bg)
    ImageView ivBg;
    @BindView(R.id.rl_person)
    RelativeLayout rlPerson;
    @BindView(R.id.tv_hozom_company_name)
    TextView tvHozomCompanyName;
    @BindView(R.id.ll_hozom_sum)
    LinearLayout llHozomSum;
    @BindView(R.id.tv_sports_company_name)
    TextView tvSportsCompanyName;
    @BindView(R.id.ll_sports_times)
    LinearLayout llSportsTimes;
    @BindView(R.id.rl_title_label)
    RelativeLayout rlTitleLabel;
    @BindView(R.id.tv_contribution_title)
    TextView tvContributionTitle;
    @BindView(R.id.srl)
    SwipeRefreshLayout srl;
    @BindView(R.id.iv_ranking)
    ImageView ivRanking;
    @BindView(R.id.tv_img_ranking_num)
    TextView tvImgRankingNum;
    private int point;
    private FireRankingAdapter fireRankingAdapter;
    private TeamFireRankingAdapter teamFireRankingAdapter;
    private TeamFireRankingAdapter teamFireRankingAdapter2;
    private String hozomName;
    private String companyTimes;
    private String companyId;
    private int page = 2;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_ranking;
    }

    @Override
    protected void initView(@Nullable Bundle savedInstanceState) {
        ButterKnife.bind(this);
        point = getIntent().getIntExtra("point", 1);
        srl.setOnRefreshListener(this);
        initViews();
        if (point == 1) {
            initProgress(point);
        } else if (point == 2) {
            companyId = getIntent().getStringExtra("companyId");
            hozomName = getIntent().getStringExtra("companyName");
            if (TextUtils.isEmpty(hozomName)) {
                tvHozomCompanyName.setText("团队火种榜");
            } else {
                tvHozomCompanyName.setText(hozomName);
            }
            if (TextUtils.isEmpty(companyId)) {
                initProgress(2);
            } else {
                initHozom();
            }
        } else {
            companyId = getIntent().getStringExtra("companyId");
            companyTimes = getIntent().getStringExtra("companyName");
            if (TextUtils.isEmpty(companyTimes)) {
                tvSportsCompanyName.setText("团队运动榜");
            } else {
                tvSportsCompanyName.setText(companyTimes);
            }
            if (TextUtils.isEmpty(companyId)) {
                initProgress(3);
            } else {
                initTeamRank();
            }
        }
    }

    /**
     * 火种排行榜
     */
    private void initHozom() {
        String string = "?companyId=%s";
        showDialog();
        new ChallengeAPI().teamHozomRank(String.format(string, companyId), new BaseResultCallback<TeamRankEntity>() {
            @Override
            public void onResponse(TeamRankEntity response) {
                dismissDialog();
                if (response.code == 0) {
                    if (response.data != null && response.data.size() > 0) {
                        teamFireRankingAdapter.setNewData(response.data);
                    }
                }
            }
        });
    }

    /**
     * 团队运动次数榜
     */
    private void initTeamRank() {
        String string = "?companyId=%s";
        showDialog();
        new ChallengeAPI().teamSportsTimes(String.format(string, companyId), new BaseResultCallback<TeamRankEntity>() {
            @Override
            public void onResponse(TeamRankEntity response) {
                dismissDialog();
                if (response.code == 0) {
                    if (response.data != null && response.data.size() > 0) {
                        teamFireRankingAdapter2.setNewData(response.data);
                    }
                }
            }
        });
    }

    private void initViews() {
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        rvRanking.setLayoutManager(layoutManager);
        fireRankingAdapter = new FireRankingAdapter(this, 1);
        fireRankingAdapter.getLoadMoreModule().setLoadMoreView(new CustomLoadMoreView());
        rvRanking.setAdapter(fireRankingAdapter);

        fireRankingAdapter.getLoadMoreModule().setAutoLoadMore(false);
        iniloadMore();

        LinearLayoutManager layoutManager1 = new LinearLayoutManager(this);
        layoutManager1.setOrientation(LinearLayoutManager.VERTICAL);
        rvRanking1.setLayoutManager(layoutManager1);
        teamFireRankingAdapter = new TeamFireRankingAdapter(this, 1);
        teamFireRankingAdapter.getLoadMoreModule().setLoadMoreView(new CustomLoadMoreView());
        rvRanking1.setAdapter(teamFireRankingAdapter);

        teamFireRankingAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                if (teamFireRankingAdapter.getData().get(position).isLowerStratum) {
                    Intent intent = new Intent(RankingActivity.this, RankingActivity.class);
                    intent.putExtra("point", 2);
                    intent.putExtra("companyId", teamFireRankingAdapter.getData().get(position).companyId);
                    intent.putExtra("companyName", teamFireRankingAdapter.getData().get(position).name);
                    startActivity(intent);
                }
            }
        });


        LinearLayoutManager layoutManager2 = new LinearLayoutManager(this);
        layoutManager2.setOrientation(LinearLayoutManager.VERTICAL);
        rvRanking2.setLayoutManager(layoutManager2);
        teamFireRankingAdapter2 = new TeamFireRankingAdapter(this, 2);
        rvRanking2.setAdapter(teamFireRankingAdapter2);

        teamFireRankingAdapter2.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                if (teamFireRankingAdapter2.getData().get(position).isLowerStratum) {
                    Intent intent = new Intent(RankingActivity.this, RankingActivity.class);
                    intent.putExtra("point", 3);
                    intent.putExtra("companyId", teamFireRankingAdapter2.getData().get(position).companyId);
                    intent.putExtra("companyName", teamFireRankingAdapter2.getData().get(position).name);
                    startActivity(intent);
                }
            }
        });


        if (point == 1) {
            ivBg.setImageResource(R.mipmap.ico_ranking_bg);
            rlPerson.setVisibility(View.VISIBLE);
            llHozomSum.setVisibility(View.GONE);
            llSportsTimes.setVisibility(View.GONE);
            llBottom.setVisibility(View.VISIBLE);
            rvRanking.setVisibility(View.VISIBLE);
            rvRanking1.setVisibility(View.GONE);
            rvRanking2.setVisibility(View.GONE);
            tvContributionTitle.setText("贡献火种(颗)");
        } else if (point == 2) {
            ivBg.setImageResource(R.mipmap.ico_team_hozom_bg1);
            rlPerson.setVisibility(View.GONE);
            llHozomSum.setVisibility(View.VISIBLE);
            llSportsTimes.setVisibility(View.GONE);
            llBottom.setVisibility(View.GONE);
            rvRanking.setVisibility(View.GONE);
            rvRanking1.setVisibility(View.VISIBLE);
            rvRanking2.setVisibility(View.GONE);
            tvContributionTitle.setText("贡献火种(颗)");
        } else {
            ivBg.setImageResource(R.mipmap.ico_team_hozom_bg2);
            rlPerson.setVisibility(View.GONE);
            llHozomSum.setVisibility(View.GONE);
            llSportsTimes.setVisibility(View.VISIBLE);
            llBottom.setVisibility(View.GONE);
            rvRanking.setVisibility(View.GONE);
            rvRanking1.setVisibility(View.GONE);
            rvRanking2.setVisibility(View.VISIBLE);
            tvContributionTitle.setText("运动(次)");
        }

    }

    private void iniloadMore() {
        fireRankingAdapter.getLoadMoreModule().setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore() {
                loadMoreRequested();
            }
        });
        fireRankingAdapter.getLoadMoreModule().setAutoLoadMore(true);
        fireRankingAdapter.getLoadMoreModule().setEnableLoadMoreIfNotFullPage(false);
    }


    /**
     * 排行榜信息
     *
     * @param point
     */
    private void initProgress(int point) {
        if (point == 1) {
            srl.setRefreshing(true);
        } else {
            showDialog();
        }
        String string = "?limit=20&page=1&type=%s";
        new ChallengeAPI().getCompanyRank(String.format(string, point), new BaseResultCallback<CompanyHozomRankEntity>() {
            @Override
            public void onResponse(CompanyHozomRankEntity response) {
                if (point == 1) {
                    srl.setRefreshing(false);
                } else {
                    dismissDialog();
                }
                if (response.code == 0) {
                    if (response.data != null) {
                        if (point == 1) {
                            switch (response.data.rank) {
                                case 1:
                                    GlideUtils.loadImage(RankingActivity.this, CommonUtil.getDrawable(R.mipmap.ico_ranking1), ivRanking);
                                    tvImgRankingNum.setText(String.valueOf(response.data.rank));
                                    ivRanking.setVisibility(View.VISIBLE);
                                    tvImgRankingNum.setVisibility(View.VISIBLE);
                                    tvRanking.setVisibility(View.GONE);
                                    break;
                                case 2:
                                    GlideUtils.loadImage(RankingActivity.this, CommonUtil.getDrawable(R.mipmap.ico_ranking2), ivRanking);
                                    tvImgRankingNum.setText(String.valueOf(response.data.rank));
                                    ivRanking.setVisibility(View.VISIBLE);
                                    tvImgRankingNum.setVisibility(View.VISIBLE);
                                    tvRanking.setVisibility(View.GONE);
                                    break;
                                case 3:
                                    GlideUtils.loadImage(RankingActivity.this, CommonUtil.getDrawable(R.mipmap.ico_ranking3), ivRanking);
                                    tvImgRankingNum.setText(String.valueOf(response.data.rank));
                                    ivRanking.setVisibility(View.VISIBLE);
                                    tvImgRankingNum.setVisibility(View.VISIBLE);
                                    tvRanking.setVisibility(View.GONE);
                                    break;
                                default:
                                    tvRanking.setVisibility(View.VISIBLE);
                                    tvRanking.setText(String.valueOf(response.data.rank));
                                    ivRanking.setVisibility(View.GONE);
                                    tvImgRankingNum.setVisibility(View.GONE);
                                    break;
                            }


                            GlideUtils.loadImage(RankingActivity.this, response.data.headImage, rciUserHeader);
                            tvUserName.setText(response.data.name);
                            tvFireNum.setText(String.valueOf(response.data.tinderCount));
                            fireRankingAdapter.setNewData(response.data.rankDtoList);
                            if (fireRankingAdapter.getData().size() >= response.data.count) {
                                fireRankingAdapter.getLoadMoreModule().loadMoreEnd(true);
                                fireRankingAdapter.getLoadMoreModule().setEnableLoadMore(false);
                            }
                        } else if (point == 2) {
                            teamFireRankingAdapter.setNewData(response.data.rankDtoList);
                        } else {
                            teamFireRankingAdapter2.setNewData(response.data.rankDtoList);
                        }
                    }
                } else {
                    ToastUtils.showShort(response.msg);
                }

            }
        });
    }

    @OnClick(R.id.btn_finish)
    public void onViewClicked() {
        finish();
    }

    @Override
    protected boolean isUseFullScreenMode() {
        return true;
    }

    @Override
    public void onRefresh() {
        page = 2;
        if (point == 1) {
            initProgress(point);
        } else {
            srl.setRefreshing(false);
            initProgress(point);
        }
    }

    public void loadMoreRequested() {
        String string = "?limit=20&page=%s&type=%s";
        new ChallengeAPI().getCompanyRank(String.format(string, page, point), new BaseResultCallback<CompanyHozomRankEntity>() {
            @Override
            public void onResponse(CompanyHozomRankEntity response) {
                if (response.code == 0) {
                    if (response.data != null) {
                        fireRankingAdapter.addData(response.data.rankDtoList);
                        if (fireRankingAdapter.getData().size() >= response.data.count) {
                            fireRankingAdapter.getLoadMoreModule().loadMoreEnd(true);
                            fireRankingAdapter.getLoadMoreModule().setEnableLoadMore(false);
                        } else {
                            page++;
                            fireRankingAdapter.getLoadMoreModule().loadMoreComplete();
                            fireRankingAdapter.getLoadMoreModule().setEnableLoadMore(true);
                        }
                    }
                } else {
                    ToastUtils.showShort(response.msg);
                }

            }
        });
    }
}
